package cn.iocoder.yudao.module.cms.benefit.dal.mysql;

import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.cms.benefit.bo.BenefitCategoryBO;
import cn.iocoder.yudao.module.cms.benefit.dal.dataobject.BenefitDO;
import cn.iocoder.yudao.module.cms.benefit.vo.BenefitPageReqVO;
import jakarta.annotation.Nonnull;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.ArrayList;
import java.util.List;

/**
 * 权益保障 Mapper
 *
 * @author 山野羡民
 */
@Mapper
public interface BenefitMapper extends CoreMapper<BenefitDO> {

    default PageResult<BenefitDO> selectPage(BenefitPageReqVO reqVO) {
        return selectPage(reqVO, new CoreLambdaQueryWrapper<BenefitDO>()
                .eqIfPresent(BenefitDO::getCategory, reqVO.getCategory())
                .betweenIfPresent(BenefitDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(BenefitDO::getCategory)
                .orderByAsc(BenefitDO::getSort)
                .orderByDesc(BenefitDO::getId));
    }

    @Nonnull
    default List<BenefitDO> selectListByCategory(String category) {
        List<BenefitDO> list = selectList(new CoreLambdaQueryWrapper<BenefitDO>()
                .eqIfPresent(BenefitDO::getCategory, category)
                .orderByAsc(BenefitDO::getSort)
                .orderByDesc(BenefitDO::getId));
        if (list == null) {
            list = new ArrayList<>();
        }
        return list;
    }

    @Select("""
                    SELECT `value` as category,`label` AS name
                    FROM `system_dict_data`
                    WHERE `dict_type`='cms_benefit_category' AND `status`=0 AND `deleted`=0
                    ORDER BY `id` DESC
            """)
    List<BenefitCategoryBO> selectAllCategory();

}
